package com.pjnkj.thunder.dagger;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pjnkj.thunder.dagger.common.Sheath;
import com.pjnkj.thunder.entity.Mapping;

//雷电的形态-匕首
public class Dagger extends Sheath {

    private String clazzString = "";

    private String uri = "";

    public Dagger(Object o) {
        //这里可以处理默认传输映射表clazz字段的加解密
        clazzString = o.toString();

        uri = Dagger.class.getPackage().getName();

        this.setMp(new Mapping());

        QueryWrapper<Mapping> qw = new QueryWrapper<Mapping>();
        qw.eq("clazz", clazzString);
        this.setMp(this.getMp().selectOne(qw));
    }

    //这里可以挥动你的匕首
    public Boolean check() {
        if (this.getMp() == null) {
            return false;
        } else {
            return true;
        }
    }

    //这里可以挥动你的匕首
    public void wave(Object o) throws Exception {

        Class inputClass = Class.forName(uri + ".convert." + mp.getInput());
        Class clazzClass = Class.forName(uri + ".service." + mp.getClazz());
        Class outputClass = Class.forName(uri + ".convert." + mp.getOutput());
        Object inputClass_Instance = inputClass.getConstructor(Object.class).newInstance(o);
        Object inputClass_InstanceOut = inputClass.getMethod("getObject").invoke(inputClass_Instance);
        Object clazzClass_Instance = clazzClass.getConstructor(Object.class).newInstance(inputClass_InstanceOut);
        Object clazzClass_InstanceOut = clazzClass.getMethod("getObject").invoke(clazzClass_Instance);
        Object outputClass_Instance = outputClass.getConstructor(Object.class).newInstance(clazzClass_InstanceOut);
        Object outputClass_InstanceOut = outputClass.getMethod("getObject").invoke(outputClass_Instance);
        this.setObject(outputClass_InstanceOut);

    }

    private Mapping mp;

    public Mapping getMp() {
        return mp;
    }

    public void setMp(Mapping mp) {
        this.mp = mp;
    }

}
